package com.isoft.controller.restful;

import java.util.List;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.isoft.common.helper.ServiceManager;
import com.isoft.domain.Girl;

/**
 * Created by yigeyanse on 2017/6/14.
 */
@RestController
@RequestMapping(value="/girl")
public class GirlController {

    private static final Logger logger = Logger.getLogger(GirlController.class);

    @RequestMapping(value="/say")
    public String sayHi(){
        logger.info("222");
        return "hi";
    }

    @RequestMapping(value = "/findGirlById/{id}")
    public Girl findGirlById(@PathVariable Integer id){
        return ServiceManager.girlService.findGirlById(id);
    }

    @RequestMapping(value="/insertGirl",method = RequestMethod.POST)
    public void insertGirl(@Valid Girl girl,BindingResult bindingResult){
    	if(bindingResult.hasErrors()) {
    		System.out.println(bindingResult.getFieldError().getDefaultMessage());
    		return;
    	}
        ServiceManager.girlService.addGirl(girl);
    }

    /**
     * 推荐
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("/index/{pageNum}")
    public List<Girl> findAllGirls(ModelMap model,@PathVariable Integer pageNum){

        Page<Girl> page = ServiceManager.girlService.findAllGirls(pageNum);
        model.put("pages",page.getPages());
        model.put("total",page.getTotal());
        System.out.println("pages:"+page.getPages()+";total:"+page.getTotal());
        return page.getResult();
    }

}
